/*
 * Created by JFormDesigner on Tue May 06 13:28:41 CST 2025
 */

package com.soft.bams.client;

import com.soft.bams.entity.Account;
import com.soft.bams.exception.LoginException;
import com.soft.bams.service.Bank;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author 21603
 */
public class LoginPanel extends JPanel {
    ATMClient atmClient;

    public LoginPanel(ATMClient atmClient) {
        this.atmClient = atmClient;
        initComponents();
    }

    private void confirm(ActionEvent e) {
        try {
            String pwd = new String(passwordField.getPassword());

            if (idTextField.getText().isEmpty() || pwd.isEmpty()) {
                JOptionPane.showMessageDialog(getParent(), "账号和密码不能为空", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }

            String command = "login#" + idTextField.getText() + "#" + pwd;
            Object response = atmClient.sendCommand(command);

            if (response instanceof Account) {
                atmClient.account= (Account) response;
                atmClient.businessPanel.initBusiness();
                CardLayout cardLayout = (CardLayout) getParent().getLayout();
                cardLayout.show(getParent(), "business");
                clearLogin();
            } else if (response instanceof String) {
                JOptionPane.showMessageDialog(getParent(), response);
            } else {
                JOptionPane.showMessageDialog(getParent(), "登录失败: 未知错误");
            }
        } catch (Exception exception) {
            JOptionPane.showMessageDialog(getParent(), "登录异常: " + exception.getMessage());
        }
    }

    private void cancel(ActionEvent e) {
        CardLayout cardLayout  = (CardLayout) this.getParent().getLayout();
        cardLayout.show(this.getParent(),"main");
    }

    public void initLogin(){
        if (atmClient.account!=null){
            idTextField.setText(String.valueOf(atmClient.account.getId()));
        }
    }

    private void clearLogin(){
        idTextField.setText("");
        passwordField.setText("");
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        idLabel = new JLabel();
        passwordLabel = new JLabel();
        idTextField = new JTextField();
        passwordField = new JPasswordField();
        confirmButton = new JButton();
        cancelButton = new JButton();

        //======== this ========
        setLayout(null);

        //---- idLabel ----
        idLabel.setText("\u7528\u6237\u8d26\u53f7\uff1a");
        add(idLabel);
        idLabel.setBounds(new Rectangle(new Point(95, 75), idLabel.getPreferredSize()));

        //---- passwordLabel ----
        passwordLabel.setText("\u7528\u6237\u5bc6\u7801\uff1a");
        add(passwordLabel);
        passwordLabel.setBounds(new Rectangle(new Point(95, 125), passwordLabel.getPreferredSize()));
        add(idTextField);
        idTextField.setBounds(185, 70, 160, idTextField.getPreferredSize().height);
        add(passwordField);
        passwordField.setBounds(185, 120, 160, passwordField.getPreferredSize().height);

        //---- confirmButton ----
        confirmButton.setText("\u786e\u5b9a");
        confirmButton.addActionListener(e -> confirm(e));
        add(confirmButton);
        confirmButton.setBounds(new Rectangle(new Point(105, 190), confirmButton.getPreferredSize()));

        //---- cancelButton ----
        cancelButton.setText("\u53d6\u6d88");
        cancelButton.addActionListener(e -> cancel(e));
        add(cancelButton);
        cancelButton.setBounds(new Rectangle(new Point(230, 190), cancelButton.getPreferredSize()));

        setPreferredSize(new Dimension(400, 300));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel idLabel;
    private JLabel passwordLabel;
    private JTextField idTextField;
    private JPasswordField passwordField;
    private JButton confirmButton;
    private JButton cancelButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
